<?php
namespace App\Http\Controllers\Laifei\Work;

use App\Http\Controllers\Controller;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LaifeiWorkStoreController extends Controller
{

    public function index()
    {

        $all = $this->all;
        $pageSize = $all['pageSize'] ?? 20;
        $pageNo = $all['pageNo'] ?? 1;
        try{
            $where = function ($query) use ($all) {
                if(isset($all['openid'])){
                    $query->where('lu.openid','=',$all['openid']);
                }
                if(isset($all['nickname'])){
                    $query->where('lu.nickname','like','%'.$all['nickname'].'%');
                }
                if(isset($all['work_no'])){
                    $query->where('ew.work_no','=',$all['work_no']);
                }
                if(isset($all['shop_name'])){
                    $query->where('shop.shop_name','like','%'.$all['shop_name'].'%');
                }
                if(isset($all['talent_name'])){
                    $query->where('talent.account_name','like','%'.$all['talent_name'].'%');
                }
                if(isset($all['channel_id']) && count($all['channel_id'])){
                    $query->whereIn('eo.channel_id',$all['channel_id']);
                }
                if(isset($all['status']) && count($all['status'])){
                    $query->whereIn('ew.status',$all['status']);
                }
                if(isset($all['contact_phone'])){
                    $query->where('shop.contact_phone','=',$all['contact_phone']);
                }
            };

            $total = DB::table('explore_work as ew')
                ->leftJoin('lottery_users as lu','ew.apply_we_id','=','lu.we_id')
                ->leftJoin('explore_order as eo','ew.eo_id','=','eo.eo_id')
                ->leftJoin('shop','ew.shop_id','=','shop.shop_id')
                ->leftJoin('talent','ew.talent_id','=','talent.talent_id')
                ->where($where)
                ->count();

            $info = DB::table('explore_work as ew')
                ->leftJoin('lottery_users as lu','ew.apply_we_id','=','lu.we_id')
                ->leftJoin('lottery_users as lu2','ew.receiver_we_id','=','lu2.we_id')
                ->leftJoin('explore_order as eo','ew.eo_id','=','eo.eo_id')
                ->leftJoin('explore_channel as ec','eo.channel_id','=','ec.channel_id')
                ->leftJoin('shop','ew.shop_id','=','shop.shop_id')
                ->leftJoin('dict_area as da','shop.district','=','da.id')
                ->leftJoin('talent','ew.talent_id','=','talent.talent_id')
                ->where($where)
                ->select('ew.work_id','ew.eo_id','ew.work_no','ew.status','ew.error_reason','ew.shop_id','ew.apply_we_id','lu.nickname as apply_name',
                    'ew.receive_time','ew.receiver_we_id','lu2.nickname as receiver_name','ew.submit_time','ew.accept_time','ew.settlement_time','ew.created_at',
                    'ec.channel_id','ec.channel_name','shop.shop_name','shop.contact_man','shop.contact_phone','shop.province','shop.city','shop.district','shop.address',
                    'da.short_name','talent.account_name as talent_name')
                ->orderByDesc('ew.created_at')
                ->skip(($pageNo - 1) * $pageSize)
                ->take($pageSize)
                ->get()
                ->map(function($item){
                    $item->status_name = Data::$exploreWorkStatus[$item->status];
                    return $item;
                })
                ->toArray();

            return $this->setReturnJson(0,'success',['total'=>$total,'info'=>$info]);

        }catch (\Exception $e){
            Log::error(LaifeiWorkStoreController::class,['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1,'查询失败',['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
        }

    }

}
